﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.yl
{
    public partial class ContractorReport : System.Web.UI.Page
    {
        DateTime useDate = new DateTime();
        string CurrentClassID = "";
        string CurrentGroupID = "";    
        protected void Page_Load(object sender, EventArgs e)
        {     
            if (!Page.IsPostBack)
            {
                Mtce_CMeasures.Text = "";
                Mtce_Content.Text = "";
                Mtce_RIfication.Text = "";
                Process_Content.Text = "";
                Process_RIfication.Text = "";
                Process_CMeasures.Text = "";
                Toutsource_Content.Text = "";
                Toutsource_RIfication.Text = "";
                Toutsource_CMeasures.Text = "";
                Toutsource_Pnums.Text = "";
                ddl_Mtc.Items.Clear();
                ddl_Process.Items.Clear();
                ddl_Toutsource.Items.Clear();
                
                if (Session["useDate"] != null)
                    useDate = DateTime.Parse(Session["useDate"].ToString());
                if (Session["ClassID"] != null)
                    CurrentClassID = Session["ClassID"].ToString();
                if (Session["GroupID"] != null)
                    CurrentGroupID = Session["GroupID"].ToString();
                if (Session["ClassID"] != null)
                {
                    DataSet ds = SelContractorByCId("日常设备维护");
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count != 0)
                    {
                        ddl_Mtc.DataSource = dt;
                        ddl_Mtc.DataTextField = "CName";
                        ddl_Mtc.DataValueField = "id";
                        ddl_Mtc.DataBind();
                    }
                    if(ddl_Mtc.Items.Count != 0)
                    {
                        Session["ContractorR"] = ddl_Mtc.SelectedItem.Text;
                        Session["ContractorRID"] = ddl_Mtc.SelectedValue;
                    }
                    
                    ds = SelContractorByCId("项目施工监管");
                    dt = ds.Tables[0];
                    if (dt.Rows.Count != 0)
                    {                       
                        ddl_Process.DataSource = dt;
                        ddl_Process.DataTextField = "CName";
                        ddl_Process.DataValueField = "id";
                        ddl_Process.DataBind(); 
                    }
                    if( ddl_Process.Items.Count != 0 )
                    {
                        Session["ContractorP"] = ddl_Process.SelectedItem.Text;
                        Session["ContractorPID"] = ddl_Process.SelectedValue;
                    }
                    
                    ds = SelContractorByCId("终端外包业务");
                    dt = ds.Tables[0];
                    if (dt.Rows.Count != 0)
                    {                    
                        ddl_Toutsource.DataSource = dt;
                        ddl_Toutsource.DataTextField = "CName";
                        ddl_Toutsource.DataValueField = "id";
                        ddl_Toutsource.DataBind();                        
                    }
                    if( ddl_Toutsource.Items.Count != 0 )
                    {
                        Session["ContractorZ"] = ddl_Toutsource.SelectedItem.Text;
                        Session["ContractorZID"] = ddl_Toutsource.SelectedValue;
                    }
                    
                }

                if (Session["ClassID"] != null || Session["GroupID"] != null || Session["useDate"] != null)
                {
                    try
                    {
                        DataTable dt_PlanMtc = new Catering_Rules.ContractorReport().GetList("ClassID='" + CurrentClassID + "' AND AddDate = '" + useDate.ToString() + "' AND GroupID = '" + CurrentGroupID + "' AND ContractorID = " + ddl_Mtc.SelectedValue).Tables[0];
                        this.Mtce_Content.Text = dt_PlanMtc.Rows[0][6].ToString();
                        this.Mtce_RIfication.Text = dt_PlanMtc.Rows[0][7].ToString();
                        this.Mtce_CMeasures.Text = dt_PlanMtc.Rows[0][8].ToString();
                        DataTable dt_PlanProcess = new Catering_Rules.ContractorReport().GetList("ClassID='" + CurrentClassID + "' AND AddDate = '" + useDate.ToString() + "' AND GroupID = '" + CurrentGroupID + "' AND ContractorID = " + ddl_Process.SelectedValue).Tables[0];
                        this.Process_Content.Text = dt_PlanProcess.Rows[0][6].ToString();
                        this.Process_RIfication.Text = dt_PlanProcess.Rows[0][7].ToString();
                        this.Process_CMeasures.Text = dt_PlanProcess.Rows[0][8].ToString();
                        DataTable dt_PlanToutsource = new Catering_Rules.ContractorReport().GetList("ClassID='" + CurrentClassID + "' AND AddDate = '" + useDate.ToString() + "' AND GroupID = '" + CurrentGroupID + "' AND ContractorID = " + ddl_Toutsource.SelectedValue).Tables[0];
                        this.Toutsource_Content.Text = dt_PlanToutsource.Rows[0][6].ToString();
                        this.Toutsource_RIfication.Text = dt_PlanToutsource.Rows[0][7].ToString();
                        this.Toutsource_CMeasures.Text = dt_PlanToutsource.Rows[0][8].ToString();
                        this.Toutsource_Pnums.Text = dt_PlanToutsource.Rows[0][9].ToString();
                    }
                    catch
                    {

                    }
                }
                if( Session["ClassName"] != null && Session["ClassName"].ToString() != "" )
                {
                    if( !Session["ClassName"].ToString().Contains("终端") )
                    {
                        tr1.Visible = false;
                        tr2.Visible = false;
                        tr3.Visible = false;
                        tr4.Visible = false;
                        tr5.Visible = false;
                    }
                    else
                    {
                        tr1.Visible = true;
                        tr2.Visible = true;
                        tr3.Visible = true;
                        tr4.Visible = true;
                        tr5.Visible = true;
                    }
                }                
            }
        }
        public DataSet SelContractorByCId(string contype)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,CName");
            strSql.Append(" FROM Contractor ");
            if (contype.Trim() != "")
            {
                strSql.Append(" where ContraType='" + contype + "'");
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        protected void ReportNo_CheckedChanged(object sender, EventArgs e)
        {
            ddl_Mtc.Enabled = false;
            this.Mtce_CMeasures.Enabled = false;
            this.Mtce_Content.Enabled = false;
            this.Mtce_RIfication.Enabled = false;
            ddl_Process.Enabled = false;
            this.Process_CMeasures.Enabled = false;
            this.Process_Content.Enabled = false;
            this.Process_RIfication.Enabled = false;
            ddl_Toutsource.Enabled = false;
            Toutsource_CMeasures.Enabled = false;
            Toutsource_Content.Enabled = false;
            Toutsource_RIfication.Enabled = false;
            Toutsource_Pnums.Enabled = false;
            this.ROCProject.Enabled = false;
            this.Del.Enabled = false;
            Bon1.Visible = false;
        }
        protected void ReportYes_CheckedChanged(object sender, EventArgs e)
        {
            ddl_Mtc.Enabled = true;
            this.Mtce_CMeasures.Enabled = true;
            this.Mtce_Content.Enabled = true;
            this.Mtce_RIfication.Enabled = true;
            ddl_Process.Enabled = true;
            this.Process_CMeasures.Enabled = true;
            this.Process_Content.Enabled = true;
            this.Process_RIfication.Enabled = true;
            ddl_Toutsource.Enabled = true;
            Toutsource_CMeasures.Enabled = true;
            Toutsource_Content.Enabled = true;
            Toutsource_RIfication.Enabled = true;
            Toutsource_Pnums.Enabled = true;
            this.ROCProject.Enabled = true;
            this.Del.Enabled = true;
            Bon1.Visible = true;
        }        

        protected void EMaintenance_Click(object sender, EventArgs e)
        {
            string strOpenUrl = "window.open('Top_Daily.aspx?mainurl=EMaintenance.aspx','_blank')";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }

        protected void EMASppPlan_Click(object sender, EventArgs e)
        {
            string strOpenUrl = "window.open('Top_Daily.aspx?mainurl=EMASppPlan.aspx','_blank')";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }

        protected void Del_Click(object sender, EventArgs e)
        {
            new Catering_Rules.ContractorReport().Delete("AddDate='" + useDate.ToString() + "' and ClassID=" + CurrentClassID + " and GroupID=" + CurrentGroupID);
            string strOpenUrl = "alert('删除成功')";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }
        protected void ROCProject_Click(object sender, EventArgs e)
        {
            string strOpenUrl = "window.open('Top_Daily.aspx?mainurl=ROCProject.aspx','_blank')";
            Session["ROCProject_ConName"] = ddl_Process.SelectedItem.Text;
            Session["ROCProject_ConID"] = ddl_Process.SelectedValue;
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }
        [WebMethod]
        public static string Save_ConReport(string Mtce_Content, string Mtce_RIfication, string Mtce_CMeasures, string Mtce_ConID, string Process_Content, string Process_RIfication, string Process_CMeasures, string Process_ConID, string Toutsource_Content, string Toutsource_RIfication, string Toutsource_CMeasures, string Toutsource_Pnums, string Toutsource_ConID, string hdn_useDate, string hdn_currentClassID, string hdn_currentGroupID)
        {
            //删除数据  
            new Catering_Rules.ContractorReport().Delete("AddDate='" + hdn_useDate + "' and ClassID=" + hdn_currentClassID + " and GroupID=" + hdn_currentGroupID + " and ContractorID=" + Mtce_ConID);
            //添加维护内容
            Catering_Model.ContractorReport _ce = new Catering_Model.ContractorReport();
            _ce.DailyManagementID = int.Parse("1");
            _ce.GroupID = int.Parse(hdn_currentGroupID);
            _ce.ClassID = int.Parse(hdn_currentClassID);
            _ce.ContractorID = int.Parse(Mtce_ConID);
            _ce.AddDate = Convert.ToDateTime(hdn_useDate);
            _ce.WorkContent = Mtce_Content;
            _ce.RiskContent = Mtce_RIfication;
            _ce.ControlContent = Mtce_CMeasures;
            _ce.optsNums = 0;
            new Catering_Rules.ContractorReport().Add(_ce);
            new Catering_Rules.ContractorReport().Delete("AddDate='" + hdn_useDate + "' and ClassID=" + hdn_currentClassID + " and GroupID=" + hdn_currentGroupID + " and ContractorID=" + Process_ConID);
            //项目施工内容
            Catering_Model.ContractorReport _ce1 = new Catering_Model.ContractorReport();
            _ce1.DailyManagementID = int.Parse("1");
            _ce1.GroupID = int.Parse(hdn_currentGroupID);
            _ce1.ClassID = int.Parse(hdn_currentClassID);
            _ce1.ContractorID = int.Parse(Process_ConID);
            _ce1.AddDate = Convert.ToDateTime(hdn_useDate);
            _ce1.WorkContent = Process_Content;
            _ce1.RiskContent = Process_RIfication;
            _ce1.ControlContent = Process_CMeasures;
            _ce1.optsNums = 0;
            new Catering_Rules.ContractorReport().Add(_ce1);
            new Catering_Rules.ContractorReport().Delete("AddDate='" + hdn_useDate + "' and ClassID=" + hdn_currentClassID + " and GroupID=" + hdn_currentGroupID + " and ContractorID=" + Toutsource_ConID);
            ////外包内容
            Catering_Model.ContractorReport _ce2 = new Catering_Model.ContractorReport();
            _ce2.DailyManagementID = int.Parse("1");
            _ce2.GroupID = int.Parse(hdn_currentGroupID);
            _ce2.ClassID = int.Parse(hdn_currentClassID);
            _ce2.ContractorID = int.Parse(Toutsource_ConID);
            _ce2.AddDate = Convert.ToDateTime(hdn_useDate);
            _ce2.WorkContent = Toutsource_Content != "undefined"? Toutsource_Content:"";
            _ce2.RiskContent = Toutsource_RIfication != "undefined" ? Toutsource_RIfication : "";
            _ce2.ControlContent = Toutsource_CMeasures != "undefined" ? Toutsource_CMeasures : "";
            _ce2.optsNums = int.Parse(Toutsource_Pnums != "undefined" ? Toutsource_Pnums : "0");            
            new Catering_Rules.ContractorReport().Add(_ce2);
            return "OK";
        }
        //验证日期内是否存在数据
        [WebMethod]
        public static string IsExists(string Mtce_ConID, string Process_ConID, string Toutsource_ConID, string hdn_useDate, string hdn_currentClassID, string hdn_currentGroupID)
        {
            DataTable dt = new Catering_Rules.ContractorReport().GetList("AddDate='" + Convert.ToDateTime(hdn_useDate).ToString() + "' and ClassID=" + hdn_currentClassID + " and GroupID=" + hdn_currentGroupID + " and ContractorID=" + Mtce_ConID).Tables[0];
            DataTable dt1 = new Catering_Rules.ContractorReport().GetList("AddDate='" + Convert.ToDateTime(hdn_useDate).ToString() + "' and ClassID=" + hdn_currentClassID + " and GroupID=" + hdn_currentGroupID + " and ContractorID=" + Process_ConID).Tables[0];
            DataTable dt2 = new Catering_Rules.ContractorReport().GetList("AddDate='" + Convert.ToDateTime(hdn_useDate).ToString() + "' and ClassID=" + hdn_currentClassID + " and GroupID=" + hdn_currentGroupID + " and ContractorID=" + Toutsource_ConID).Tables[0];
            return (dt != null && dt.Rows.Count > 0 && dt1 != null && dt1.Rows.Count > 0&&dt2 != null && dt2.Rows.Count > 0) ? "OK" : "NO";
        }

        protected void ddl_Mtc_SelectedIndexChanged(object sender, EventArgs e)
        {
            Mtce_CMeasures.Text = "";
            Mtce_Content.Text = "";
            Mtce_RIfication.Text = "";
            try
            {
                DataTable dt_PlanMtc = new Catering_Rules.ContractorReport().GetList("ClassID='" + CurrentClassID + "' AND AddDate = '" + useDate.ToString() + "' AND GroupID = '" + CurrentGroupID + "' AND ContractorID = " + ddl_Mtc.SelectedValue).Tables[0];
                this.Mtce_Content.Text = dt_PlanMtc.Rows[0][6].ToString();
                this.Mtce_RIfication.Text = dt_PlanMtc.Rows[0][7].ToString();
                this.Mtce_CMeasures.Text = dt_PlanMtc.Rows[0][8].ToString();                
            }
            catch
            {

            }
            if (ddl_Process.Items.Count != 0)
            {
                Session["ContractorR"] = ddl_Process.SelectedItem.Text;
                Session["ContractorRID"] = ddl_Process.SelectedValue;
            }
        }

        protected void ddl_Process_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process_Content.Text = "";
            Process_RIfication.Text = "";
            Process_CMeasures.Text = "";
            try
            {               
                DataTable dt_PlanProcess = new Catering_Rules.ContractorReport().GetList("ClassID='" + CurrentClassID + "' AND AddDate = '" + useDate.ToString() + "' AND GroupID = '" + CurrentGroupID + "' AND ContractorID = " + ddl_Process.SelectedValue).Tables[0];
                this.Process_Content.Text = dt_PlanProcess.Rows[0][6].ToString();
                this.Process_RIfication.Text = dt_PlanProcess.Rows[0][7].ToString();
                this.Process_CMeasures.Text = dt_PlanProcess.Rows[0][8].ToString();                
            }
            catch
            {

            }
            if (ddl_Process.Items.Count != 0)
            {
                Session["ContractorP"] = ddl_Process.SelectedItem.Text;
                Session["ContractorPID"] = ddl_Process.SelectedValue;
            }
        }

        protected void ddl_Toutsource_SelectedIndexChanged(object sender, EventArgs e)
        {
            Toutsource_Content.Text = "";
            Toutsource_RIfication.Text = "";
            Toutsource_CMeasures.Text = "";
            Toutsource_Pnums.Text = "";
            try
            {                
                DataTable dt_PlanToutsource = new Catering_Rules.ContractorReport().GetList("ClassID='" + CurrentClassID + "' AND AddDate = '" + useDate.ToString() + "' AND GroupID = '" + CurrentGroupID + "' AND ContractorID = " + ddl_Toutsource.SelectedValue).Tables[0];
                this.Toutsource_Content.Text = dt_PlanToutsource.Rows[0][6].ToString();
                this.Toutsource_RIfication.Text = dt_PlanToutsource.Rows[0][7].ToString();
                this.Toutsource_CMeasures.Text = dt_PlanToutsource.Rows[0][8].ToString();
                this.Toutsource_Pnums.Text = dt_PlanToutsource.Rows[0][9].ToString();
            }
            catch
            {

            }
            if (ddl_Toutsource.Items.Count != 0)
            {
                Session["ContractorZ"] = ddl_Toutsource.SelectedItem.Text;
                Session["ContractorZID"] = ddl_Toutsource.SelectedValue;
            }
        }
          
    }
}